/*
 * @Description: 文件内容描述
 * @Author: 李志杰
 * @Date: 2020-03-13 23:17:54
 * @LastEditTime: 2020-03-19 17:22:53
 * @LastEditors: huacong
 */
import _ from 'lodash';
import * as api from '@/services/ReportMgr';
import * as utils from '../utils/treeUtils';

const Model = {
  namespace: 'PriceManageEntireProjectItems',
  state: {
    all: [],
    expandedRowKeys: [],
    tree: [],
  },
  effects: {
    *init({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: { all: [], expandedRowKeys: [], tree: [] },
      });
      const response = yield call(api.priceReportUsingGET, payload);
      if (response && response.data && !_.isEmpty(response.data)) {
        // console.log(_.uniqBy(response.data, 'orgId'), 'all')
        const all = utils.buildRowStructure(response.data);

        const expandedRowKeys = utils.getExpandedRowKeys(all);
        const tree = utils.buildTree(all);
        yield put({
          type: 'save',
          payload: { all, tree, expandedRowKeys },
        });
      }
      if (callback) callback(response && response.data ? response.data : []);
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
